#!/usr/bin/env python3
from WTimer import WTimer


class Controller(object):

    GOOD = 0
    SOSO = 1
    BAD = 2
    TERRIBLE = 3

    def __init__(self):
        self._map = {}
        self._timer = WTimer(self._check, 1800)
        self._timer.start()

    def add(self, city, led):
        self._map[city] = led
        self._check()

    def _check(self):
        for city in self._map:
            level = self._air_level(city)
            self._turn_light(self._map[city], level)

    def _air_level(self, city):
        aqi = city.aqi
        if aqi <= 150:
            return self.GOOD
        elif aqi <= 200:
            return self.SOSO
        elif aqi <= 250:
            return self.BAD
        elif aqi >= 300:
            return self.TERRIBLE
        else:
            assert False

    def _turn_light(self, light, level):
        if level == self.GOOD:
            light.off()
        elif level == self.SOSO:
            light.on()
        elif level == self.BAD:
            light.on(0.6)
        elif level == self.TERRIBLE:
            light.on(0.1)
        else:
            assert False
